package Tudy

import (
	"fmt"
	"strconv"
	"strings"
)

//在更改的时候把S去掉
var ActionMapS map[string]map[string]string
var GoToMapS map[string]map[string]string
var ActionStringS =[]string{"i","+","*","(",")","#"}
var GoToStringS=[]string{"E","T","F"}
var GrammerS map[string]string
var LengthGramerS map[string]string
func init(){
	ActionMap=make(map[string]map[string]string)
	ActionMap["0"]=make(map[string]string)
	ActionMap["0"]["i"]="S5"
	ActionMap["0"]["("]="S4"
	ActionMap["1"]=make(map[string]string)
	ActionMap["1"]["+"]="S6"
	ActionMap["1"]["#"]="acc"
	ActionMap["2"]=make(map[string]string)
	ActionMap["2"]["+"]="r2"
	ActionMap["2"]["*"]="S7"
	ActionMap["2"][")"]="r2"
	ActionMap["2"]["#"]="r2"
	ActionMap["3"]=make(map[string]string)
	ActionMap["3"]["+"]="r4"
	ActionMap["3"]["*"]="r4"
	ActionMap["3"][")"]="r4"
	ActionMap["3"]["#"]="r4"
	ActionMap["4"]=make(map[string]string)
	ActionMap["4"]["i"]="S5"
	ActionMap["4"]["("]="S4"
	ActionMap["5"]=make(map[string]string)
	ActionMap["5"]["+"]="r6"
	ActionMap["5"]["*"]="r6"
	ActionMap["5"][")"]="r6"
	ActionMap["5"]["#"]="r6"
	ActionMap["6"]=make(map[string]string)
	ActionMap["6"]["i"]="S5"
	ActionMap["6"]["("]="S4"
	ActionMap["7"]=make(map[string]string)
	ActionMap["7"]["i"]="S5"
	ActionMap["7"]["("]="S4"
	ActionMap["8"]=make(map[string]string)
	ActionMap["8"]["+"]="S6"
	ActionMap["8"][")"]="S11"
	ActionMap["9"]=make(map[string]string)
	ActionMap["9"]["+"]="r1"
	ActionMap["9"]["*"]="S7"
	ActionMap["9"][")"]="r1"
	ActionMap["9"]["#"]="r1"
	ActionMap["10"]=make(map[string]string)
	ActionMap["10"]["+"]="r3"
	ActionMap["10"]["*"]="r3"
	ActionMap["10"][")"]="r3"
	ActionMap["10"]["#"]="r3"
	ActionMap["11"]=make(map[string]string)
	ActionMap["11"]["+"]="r5"
	ActionMap["11"]["*"]="r5"
	ActionMap["11"][")"]="r5"
	ActionMap["11"]["#"]="r5"
	GoToMap=make(map[string]map[string]string)
	GoToMap["0"]=make(map[string]string)
	GoToMap["0"]["E"]="1"
	GoToMap["0"]["T"]="2"
	GoToMap["0"]["F"]="3"
	GoToMap["1"]=make(map[string]string)
	GoToMap["2"]=make(map[string]string)
	GoToMap["3"]=make(map[string]string)
	GoToMap["4"]=make(map[string]string)
	GoToMap["4"]["E"]="8"
	GoToMap["4"]["T"]="2"
	GoToMap["4"]["F"]="3"
	GoToMap["5"]=make(map[string]string)
	GoToMap["6"]=make(map[string]string)
	GoToMap["6"]["T"]="9"
	GoToMap["6"]["F"]="3"
	GoToMap["7"]=make(map[string]string)
	GoToMap["7"]["F"]="10"
	GoToMap["8"]=make(map[string]string)
	GoToMap["9"]=make(map[string]string)
	GoToMap["10"]=make(map[string]string)
	GoToMap["11"]=make(map[string]string)
	Grammer=make(map[string]string)
	Grammer["E+T"]="E"
	Grammer["T"]="E"
	Grammer["T*F"]="T"
	Grammer["F"]="T"
	Grammer["(E)"]="F"
	Grammer["i"]="F"
	LengthGramer=make(map[string]string)
	LengthGramer["1"]="E+T"
	LengthGramer["2"]="T"
	LengthGramer["3"]="T*F"
	LengthGramer["4"]="F"
	LengthGramer["5"]="(E)"
	LengthGramer["6"]="i"
}
func mainA() {
	fmt.Println("请输入句子")
	var str string
	fmt.Scan(&str)
	str+="#"
	StateStack:=make([]string,0)
	StateStack=append(StateStack,"0")
	SymbolStack:=make([]string,0)
	SymbolStack=append(SymbolStack,"#")
	InputStack:=make([]string,0)
	for i:=0;i<len(str);i++{
		InputStack=append(InputStack,string(str[i]))
	}
	ActionStack:=make([]string,0)
	GoTostack:=make([]string,0)
	results:=make([]string,0)
	resultActionGoto:=make([]string,0)
	resultLanguage:=make([]string,0)
	resultLanguage=append(resultLanguage,"-")
	flagBefore:=""
	for{
		flag:=""
		if byte(InputStack[0][0])>'0'&&byte(InputStack[0][0])<'9'{
			flag=InputStack[0]
			//resultLanguage=append(resultLanguage,string(InputStack[0][0]))
			InputStack[0]=string('i')
		}else{
			//resultLanguage=append(resultLanguage,"-")
			flag=InputStack[0]
		}

		ActionNext:=ActionMap[StateStack[len(StateStack)-1]][InputStack[0]]
		ActionStack=append(ActionStack,ActionNext)
		if string(ActionNext[0])=="S"{
			NextNum:=ActionNext[1:]
			StateStack=append(StateStack,NextNum)
			SymbolStack=append(SymbolStack,flag)
			flagBefore=flag
			InputStack=InputStack[1:]
			GoTostack=append(GoTostack,"")
			resultLanguage=append(resultLanguage,"-")
		}else if string(ActionNext[0])=="r"{
			NextPop:=ActionNext[1:]
			PopString:=LengthGramer[NextPop]//b
			ChangeString:=Grammer[PopString]//A
			StateStack=StateStack[0:len(StateStack)-len(PopString)]
			SymbolStack=SymbolStack[0:len(SymbolStack)-len(PopString)]
			SymbolStack=append(SymbolStack,ChangeString)
			PopNum:=StateStack[len(StateStack)-1]
			NextNum:=GoToMap[PopNum][ChangeString]
			StateStack=append(StateStack,NextNum)
			GoTostack=append(GoTostack,NextNum)
			stackResultLanguage:=resultLanguage[len(resultLanguage)-len(PopString):]
			resultLanguage=resultLanguage[0:len(resultLanguage)-len(PopString)]
			if NextPop=="1"{
				left:=stackResultLanguage[0]
				right:=stackResultLanguage[2]
				ansNum,_:=strconv.Atoi(left)
				ansNumRight,_:=strconv.Atoi(right)
				ansNum+=ansNumRight
				resultLanguage=append(resultLanguage,strconv.Itoa(ansNum))
				flagBefore=strconv.Itoa(ansNum)
			}else if NextPop=="3"{
				left:=stackResultLanguage[0]
				right:=stackResultLanguage[2]
				ansNum,_:=strconv.Atoi(left)
				ansNumRight,_:=strconv.Atoi(right)
				ansNum*=ansNumRight
				resultLanguage=append(resultLanguage,strconv.Itoa(ansNum))
				flagBefore=strconv.Itoa(ansNum)
			}else if NextPop=="5"{
				right:=stackResultLanguage[1]
				ansNumRight,_:=strconv.Atoi(right)
				resultLanguage=append(resultLanguage,strconv.Itoa(ansNumRight))
				flagBefore=strconv.Itoa(ansNumRight)
			} else{
				resultLanguage=append(resultLanguage,flagBefore)
			}
		}
		results=append(results,strings.Join(StateStack,"")+"  "+strings.Join(resultLanguage,"")+"     "+strings.Join(SymbolStack,"")+"     "+strings.Join(InputStack,""))
		resultActionGoto=append(resultActionGoto,ActionStack[len(ActionStack)-1]+" "+GoTostack[len(GoTostack)-1])
		if ActionStack[len(ActionStack)-1]=="acc"{
			break
		}
	}
	fmt.Println("状态栈   ","语义栈  ","符号栈   ","输入串   ","Action   ","GoTo")
	fmt.Println("0","       #  ","-        ",str+"    ",resultActionGoto[0])
	for i:=0;i<len(results)-1;i++{
		fmt.Println(results[i],"  ",resultActionGoto[i+1])
	}
}
